<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="合同编号" prop="contractNo">
        <el-input
          v-model="queryParams.contractNo"
          placeholder="请输入合同编号"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:contract:transactionType:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="合同编号" align="center" prop="contractNo" />
      <el-table-column label="交款人卖方" align="center" prop="buyerName" />
      <el-table-column
        label="房屋坐落位置"
        align="center"
        prop="propertyLocation"
      />
      <el-table-column
        label="履约保证金(元)"
        align="center"
        prop="performanceBondAmount"
      >
        <template #default="scope">
          <span>{{
            scope.row.performanceBondAmount
              ? `${scope.row.performanceBondAmount}元`
              : `0元`
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="物业保证金(元)"
        align="center"
        prop="propertyInsuranceAmount"
      >
        <template #default="scope">
          <span>{{
            scope.row.propertyInsuranceAmount
              ? `${scope.row.propertyInsuranceAmount}元`
              : `0元`
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="签约店面" align="center">
        <template #default="scope">
          {{ scope.row.regionName + "" + scope.row.storeName }}
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="applicantName" />
      <!-- 是否展示 -->
      <el-table-column label="是否打开账单" align="center" prop="isReviewed">
        <template #default="scope">
          <el-switch
            v-model="scope.row.isReviewed"
            :active-value="2"
            :inactive-value="1"
            @change="handlePaymentStatusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.addTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="180"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.isReviewed == 1"
            v-hasPermi="['system:contract:transactionOrder:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Printer"
            @click="handlePrint(scope.row)"
            v-hasPermi="['system:contract:transactionOrder:print']"
            >打印</el-button
          >
          <el-button
            link
            type="primary"
            @click="handleDetail(scope.row)"
            v-hasPermi="['']"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <BuyerPaymentNotice v-if="open" v-model:fData="form" />
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 查看 -->
    <el-dialog
      :title="title"
      v-model="openDetail"
      width="1000px"
      append-to-body
    >
      <SellerPaymentDetail v-if="openDetail" v-model:fData="form" />
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="openDetail = false"
            >确 定</el-button
          >
          <el-button @click="openDetail = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 打印 -->
    <el-dialog
      title="打印"
      v-model="printComfirm"
      width="1000px"
      append-to-body
    >
      <div>
        <div class="form-wrapper" ref="printRef">
          <h2 class="title">交款通知单</h2>

          <div class="row">
            合同编号：<span class="line short">{{ form.contractNo }}</span>
          </div>
          <div class="row">
            今有 <span class="line short">{{ form.buyerName }}</span> 交付位于
            <span class="line long">{{ form.propertyLocation }}</span> 房屋的
          </div>

          <div class="row">
            1、卖方佣金 ￥<span class="line amount">{{
              form.serviceFeeDue +
              form.transactionFeeDue +
              form.agnetServiceFeeDue
            }}</span>
            代收￥<span class="line amount">
              {{
                form.serviceFeeProxyDue +
                form.transactionFeeProxyDue +
                form.agnetServiceFeeCollectDue
              }}
            </span>
            合计￥<span class="line amount">{{
              form.serviceFeeDue +
              form.transactionFeeDue +
              form.agnetServiceFeeDue +
              form.serviceFeeProxyDue +
              form.transactionFeeProxyDue +
              form.agnetServiceFeeCollectDue
            }}</span>
          </div>

          <div class="row">备注：<span class="line xlong"></span></div>

          <div class="row">
            2、履约保证金￥<span class="line amount">{{
              form.performanceBondAmount
            }}</span>
            物业保障金￥<span class="line amount">{{
              form.propertyInsuranceAmount
            }}</span>
          </div>

          <!-- <div class="row">
            3、首付款 ￥<span class="line amount">{{
              form.housePriceAmount
            }}</span>
          </div> -->

          <div class="row mt40">
            签约店面：<span class="line">{{ form.signingStore }}</span>
          </div>
          <div
            class="row"
            style="display: flex; justify-content: space-between"
          >
            <div>
              签约员：<span class="line medium">{{ form.applicantName }}</span>
            </div>
            <div>财务部：<span class="line medium">{{}}</span></div>
          </div>
          <div
            class="row"
            style="display: flex; justify-content: space-between"
          >
            <div>
              <span>{{ currentDate }}</span>
            </div>
            <div>
              <span>{{ currentDate }}</span>
            </div>
          </div>
        </div>
      </div>
      <template #footer>
        <el-button @click="printComfirm = false">取 消</el-button>
        <el-button type="primary" @click="printNoticeForm">打 印</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="paymentSeller">
import {
  listPayMent,
  queryPayMent,
  editPayMent,
  isShowPayMent,
} from "@/api/business/paymentSeller";
import BuyerPaymentNotice from "./components/BuyerPaymentNotice.vue";
import SellerPaymentDetail from "./components/SellerPaymentDetail.vue";
const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
import { reactive } from "vue";
const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    type: 1,
    contractNo: "",
  },
  rules: {},
});

const { queryParams, rules } = toRefs(data);
const form = ref({});

/** 查询岗位列表 */
function getList() {
  loading.value = true;
  listPayMent(queryParams.value).then((response) => {
    dataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
// 是否展示
function handlePaymentStatusChange(row) {
  let text = row.isReviewed == "2" ? "开启" : "关闭";
  const data = {
    id: row.id,
    isReviewed: row.isReviewed,
  };
  proxy.$modal
    .confirm("确认要" + text + "吗?")
    .then(function () {
      return isShowPayMent(data);
    })
    .then(() => {
      proxy.$modal.msgSuccess(text + "成功");
    })
    .catch(function () {
      row.isReviewed = row.isReviewed == "2" ? 1 : 2;
    });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {};
  proxy.resetForm("postRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.postId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const id = row.id || ids.value;
  const params = {
    id,
    type: queryParams.value.type,
  };
  queryPayMent(params).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改";
  });
}
const openDetail = ref(false);
function handleDetail(row) {
  const id = row.id || ids.value;
  const params = {
    id,
    type: queryParams.value.type,
  };
  queryPayMent(params).then((response) => {
    form.value = response.data;
    openDetail.value = true;
  });
}
/** 提交按钮 */
function submitForm() {
  if (
    !form.value.serviceFeeDueTime ||
    !form.value.transactionFeeDueTime ||
    !form.value.agnetServiceFeeDueTime ||
    !form.value.serviceFeeProxyDueTime ||
    !form.value.transactionFeeProxyDueTime ||
    !form.value.agnetServiceFeeCollectDueTime
  ) {
    proxy.$modal.msgError("请选择应交时间");
    return;
  }
  editPayMent(form.value).then((res) => {
    open.value = false;
    proxy.$modal.msgSuccess(res.msg);
  });
}
const printComfirm = ref(false);
function handlePrint(row) {
  const id = row.id || ids.value;
  const params = {
    id,
    type: queryParams.value.type,
  };
  queryPayMent(params).then((response) => {
    form.value = response.data;
    printComfirm.value = true;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/post/export",
    {
      ...queryParams.value,
    },
    `post_${new Date().getTime()}.xlsx`
  );
}

getList();
const printRef = ref(null);

const currentDate = `${new Date().getFullYear()}年${
  new Date().getMonth() + 1
}月${new Date().getDate()}日`;

const printNoticeForm = () => {
  const printContent = printRef.value.innerHTML;
  const printWindow = window.open("", "", "width=800,height=600");
  printWindow.document.write(`
    <html>
      <head>
        <title>打印</title>
        <style>
          body { font-family: SimSun, serif; padding: 20px; font-size: 16px; color: #000; }
          .title { text-align: center; font-weight: bold; font-size: 22px; margin-bottom: 20px; }
          .row { margin: 8px 0; }
          .checkbox { display: inline-block; width: 16px; height: 16px; line-height: 16px; text-align: center; border: 1px solid #000; margin: 0 4px; vertical-align: middle; }
          .line { display: inline-block; border-bottom: 1px solid #000;  margin: 0 4px; text-align: center; }
          .line.short { min-width: 100px; }
          .line.medium { min-width: 120px; }
          .line.long { min-width: 280px; }
          .line.xlong { width: 520px; height: 22px; }
          .line.amount { width: 80px; }
          .mt40 { margin-top: 40px; }
          .date-row { text-align: right; margin-top: 10px; }
        </style>
      </head>
      <body>
        ${printContent}
      </body>
    </html>
  `);
  printWindow.document.close();
  printWindow.focus();
  printWindow.print();
  // printWindow.close();
};
</script>
<style scoped>
.buyer-payment-form {
  font-family: SimSun, serif;
  padding: 20px;
  font-size: 14px;
  background: #fff;
  width: 900px;
  margin: 0 auto;
  border: 1px solid #000;
}
.title {
  text-align: center;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 20px;
}
.table {
  display: table;
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 12px;
}
.table-row {
  display: table-row;
}
.cell {
  display: table-cell;
  border: 1px solid #000;
  padding: 6px 10px;
  vertical-align: middle;
  min-height: 30px;
}
.header {
  background: #f0f0f0;
  font-weight: bold;
  text-align: center;
}
.input {
  padding: 4px 6px;
}
.label {
  background: #f9f9f9;
  font-weight: bold;
  width: 140px;
}
.readonly {
  background: #f9f9f9;
  text-align: center;
}
.subtotal {
  font-weight: bold;
  background: #f4f8ff;
  text-align: left;
}
.total-table .total {
  font-weight: bold;
  background: #e1f3ff;
  text-align: center;
}
.group-table {
  margin-top: 10px;
}
.form-wrapper {
  width: 720px;
  margin: 0 auto;
  font-family: "SimSun", serif;
  font-size: 16px;
  color: #000;
  line-height: 1.8;
  padding: 20px;
}

.title {
  text-align: center;
  font-weight: bold;
  font-size: 22px;
  margin-bottom: 20px;
}

.row {
  margin: 8px 0;
}

.checkbox {
  display: inline-block;
  width: 16px;
  height: 16px;
  line-height: 16px;
  text-align: center;
  border: 1px solid #000;
  margin: 0 4px;
  vertical-align: middle;
}
.line {
  display: inline-block;
  border-bottom: 1px solid #000;
  /* vertical-align: middle; */
  margin: 0 4px;
  text-align: center;
}

.line.short {
  min-width: 100px;
}

.line.medium {
  width: 120px;
}

.line.long {
  min-width: 280px;
}

.line.xlong {
  width: 520px;
  height: 22px;
}

.line.amount {
  width: 80px;
}

.mt40 {
  margin-top: 40px;
}

.date-row {
  text-align: right;
  margin-top: 10px;
}

.btn-print {
  margin-bottom: 10px;
  text-align: right;
}
.buyer-payment-form {
  font-family: SimSun, serif;
  padding: 20px;
  font-size: 14px;
  background: #fff;
  width: 900px;
  margin: 0 auto;
  border: 1px solid #000;
}
.title {
  text-align: center;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 20px;
}
.table {
  display: table;
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 12px;
}
.table-row {
  display: table-row;
}
.cell {
  display: table-cell;
  border: 1px solid #000;
  padding: 6px 10px;
  vertical-align: middle;
  min-height: 30px;
}
.header {
  background: #f0f0f0;
  font-weight: bold;
  text-align: center;
}
.input {
  padding: 4px 6px;
}
.label {
  background: #f9f9f9;
  font-weight: bold;
  width: 140px;
}
.readonly {
  background: #f9f9f9;
  text-align: center;
}
.subtotal {
  font-weight: bold;
  background: #f4f8ff;
  text-align: left;
}
.total-table .total {
  font-weight: bold;
  background: #e1f3ff;
  text-align: center;
}
.group-table {
  margin-top: 10px;
}
</style>
